package drds.configuration.db.xml_mapping.dynamic_tag.sql_template;

import drds.configuration.db.xml_mapping.configuration.Configuration;
import drds.configuration.db.xml_mapping.dynamic_tag.tag.Tag;
import drds.configuration.db.xml_mapping.dynamic_tag.tag_context.TagContext;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;


public class RawSqlTemplate extends AbstractSqlTemplate {
    @Setter
    @Getter
    private final SqlTemplate sqlTemplate;

    public RawSqlTemplate(Configuration configuration, Tag rootTag) {
        this(configuration, getSql(configuration, rootTag));
    }

    public RawSqlTemplate(Configuration configuration, String sql) {
        super(configuration);
        this.sqlTemplate = parse(sql);
    }

    private static String getSql(Configuration configuration, Tag rootTag) {
        TagContext tagContext = new TagContext(null);
        rootTag.apply(tagContext);
        return tagContext.getSql();
    }

    @Override
    public Bind bindWith(Map<String, Object> parameterObject) {
        return sqlTemplate.bindWith(parameterObject);
    }

}
